Ext.define('MyApp.controller.Main', {
	extend : 'MyApp.controller.share.BaseGridController',
	views : ['ContentPanel', 'Header', 'Navigation', 'Viewport',
			'ux.ButtonTransparent', 'uploadimage.UploadImageForm',
			'user.PersonallyInfo', 'user.ChangePassword', 'ux.BaseForm',
			'ux.BasePanelView','ux.BaseAuthorityButton'],
	stores : ['NavigationMenuStore'],
	models : ['ToolbarMenuEntity', 'UserEntity'],
	refs : [{
				ref : 'viewport',
				selector : 'viewport'
			}, {
				ref : 'navigation',
				selector : 'navigation'
			}, {
				ref : 'contentPanel',
				selector : 'contentPanel'
			}],
	requires : ['MyApp.share.MyUtil', 'MyApp.share.MessageBoxUtil'],
	init : function() {
		/**
		 * 同步请求获取用户信息
		 */
		Ext.Ajax.request({
					url : 'user/getCurrentUser',
					async : false,// 同步请求数据
					success : function(response) {
						Ext.user = Ext.create('MyApp.model.UserEntity', Ext
										.decode(response.responseText));
					},
					failure : function(response, options) {
						location.href = "login";
					}
				});
		this.control({
			'personallyInfo button#cancel' : {
				click : 'onCancelButtonClick'
			},
			'changepassword button#cancel' : {
				click : 'onCancelButtonClick'
			},
			'changepassword button#save' : {
				click : function(btn) {
					var win = btn.up('window');
					var form = win.down('form').getForm();
					if (form.isValid()) {
						form.submit({
									url : 'user/changePasswod',
									submitEmptyText : false,
									success : function(fp, o) {
										var text = Ext
												.decode(o.response.responseText);
										if (text.success != '0') {
											Ext.MessageBoxUtil.msg('提示',
													'操作成功!');
											win.close();
										} else {
											Ext.MessageBoxUtil.msg('提示',
													'操作失败,请重试!');
										}
									},
									failure : function(form, action) {
										Ext.MessageBoxUtil.msg('提示',
												'操作失败,服务器发生错误!');
									}
								});
					}
				}
			},
			'personallyInfo button#save' : {
				click : function(btn) {
					var win = btn.up('window');
					var form = win.down('form').getForm();
					if (form.isValid()) {
						var record = form.getRecord();
						if (record) {
							record.beginEdit();
							record.set(form.getValues());
							record.endEdit();
							record.save({
								success : function(m, response) {
									var text = Ext
											.decode(response.response.responseText);
									if (text == '0') {
										Ext.MessageBoxUtil.msg('提示', '数据提交失败!');
									} else {
										Ext.MessageBoxUtil.msg('提示', '数据提交成功!');
										win.close();
									}
								}
							});
						}
					}
				}
			},
			'navigation' : {
				load : function(t) {
					if (t.getRootNode().firstChild) {
						t.getRootNode().firstChild.expand();
					}
				},
				selectionchange : 'onNavSelectionChange'
			},
			/**
			 * 刷新导航面板
			 */
			'navigation tool[type=refresh]' : {
				click : function(t) {
					this.getNavigation().getStore().reload({
						scope : this,
						callback : function(records, operation, success) {
							var activeTab = this.getContentPanel()
									.getActiveTab();
							this.activeTabOpenTree(activeTab);
						}
					});
				}
			},
			'appHeader #personallyinfo' : {
				click : function() {
					var win = Ext.widget('personallyInfo', {
								animateTarget : document.body
							});
					win.down('form').getForm().loadRecord(Ext.user);
					win.show();
				}
			},
			'appHeader #changepassword' : {
				click : function() {
					Ext.widget('changepassword', {
								animateTarget : document.body
							}).show();
				}
			},
			'appHeader button#home' : {
				click : function(bth) {
					this.getContentPanel().setActiveTab(0);
				}
			},
			'appHeader button#loginOut' : {
				click : function(bth) {
					Ext.MessageBox.confirm('提示', '确定要退出系统吗?',
							function showResult(btnResult) {
								if (btnResult == 'yes') {
									Ext.Ajax.request({
												url : 'loginOut',
												success : function(response) {
													var text = response.responseText;
													if (text == 1) {
														location.href = "login";
													} else {
														Ext.MessageBoxUtil.msg(
																'提示', '注销失败!');
													}
												},
												failure : function(response,
														options) {
													Ext.MessageBoxUtil
															.msg(
																	'提示',
																	'操作失败,错误编号：'
																			+ response.status);
												}
											});
								}
							});
				}
			},
			'contentPanel tab' : {
				activate : 'onContentPanelActivate'
			}
		});
	},
	/**
	 * tab活动时 对应打开 树节点
	 * 
	 * @param {}
	 *            tab
	 */
	activeTabOpenTree : function(tab) {
		var record = this.getNavigationMenuStoreStore().getNodeById(tab.id);
		if (record) {
			record.bubble(function(a) {
						if (!a.isExpanded()) {
							a.expand();
						}
					});
			this.getNavigation().getSelectionModel().select(record);
		} else {
			this.getNavigation().getSelectionModel().deselectAll();
		}
	},
	onNavSelectionChange : function(selModel, records) {
		Ext.myUtil.loadContent(records, this.getContentPanel(),
				this.application);
	},
	onContentPanelActivate : function(tab) {
		if ('nav' == tab.card.cls) {
			// 更新浏览器URL
			document.title = document.title.split(' - ')[0] + ' - ' + tab.text;
			// location.hash = xtype;
			this.activeTabOpenTree(tab.card);
		}
	}
});